package com.ecp.user.service;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.ecp.commons.db.GenericServiceImpl;
import com.ecp.crm.bean.User;
import com.ecp.user.bean.PayPasswordBinding;
import com.ecp.util.MD5;

@Service(value = "passwordBindingService")
public class PayPasswordBindingServiceImpl extends GenericServiceImpl<PayPasswordBinding> implements
		PayPasswordBindingService {

	@Override
	public String getPayPassword(User user) {
		PayPasswordBinding bean = null;
		String sql = "select * from tbl_paypasswordbinding where USERID = ?";
		try {
			bean = this.getEntity(sql, new Object[] { user.getId() });
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bean == null ? null : bean.getPassword();
	}

	@Override
	public boolean check_pay_pwd(String pay_pwd, User user, HttpServletRequest request) throws Exception {
		boolean ok = false;
		if(StringUtils.isEmpty(this.getPayPassword(user))){
			throw new Exception("您尚未设置支付密码");
		}
		String pwd_1 = MD5.getMD5Str(this.getPayPassword(user).toUpperCase() + "pinzhi365");
		if (pay_pwd.equalsIgnoreCase(pwd_1)) {
			ok = true;
		} else {
			throw new Exception("支付密码错误");
		}
		return ok;
	}

}
